<?php

function saveVideo() {
	$dbParams = parse_ini_file('db.properties');

	$dbhost = $dbParams['host'];
	$dbuser = $dbParams['user'];
	$dbpass = $dbParams['pwd'];

	$conn = mysql_connect($dbhost, $dbuser, $dbpass)
	or die ('Error connecting to mysql ' . mysql_error());

	$dbname = $dbParams['name'];
	mysql_select_db($dbname);

	$videoId = $_GET["videoId"];
	echo $tag . "\n";
	$tags = split(",", str_replace("_", " ", $_GET["tags"]));
	echo $videos . "\n";
		
	$statement = 'select id from video where videoId="' . $videoId . '"';
	echo $statement . "\n";
	$res = mysql_query($statement);
	$id_res = mysql_fetch_array($res);
	if(is_null($id_res) || empty($id_res)) {
		$statement = 'insert into video (video_id) values ("' . $videoId . '")';
		echo $statement . "\n";
		$res = mysql_query($statement);
		echo "Result: " . $res . "\n";
		$statement = 'select id from video where video_id="' . $videoId . '"';
		echo $statement . "\n";
		$res = mysql_query($statement);
		$id_res = mysql_fetch_array($res);
	}
	$video_id = $id_res[0];
	echo "VideoId: " . $video_id . "\n";
	
	foreach($tags as $tag) {
		$tag = trim($tag);
		$statement = 'select id from tag where name="' . $tag . '"';
		echo $statement . "\n";
		$res = mysql_query($statement);
		$id_res = mysql_fetch_array($res);
		if(is_null($id_res) || empty($id_res))
			continue;
		$tag_id = $id_res[0];
		echo "TagId: " . $tag_id . "\n";
		$statement = 'insert into video_tag (video_id, tag_id) values ("' . $video_id . '", "' . $tag_id . '")';
		echo $statement . "\n";
		$res = mysql_query($statement);
		echo "Result: " . $res . "\n";
	}
	
	mysql_free_result($res);
	mysql_close($conn);
}

function getVideo() {
	if(!is_null($_GET["tags"]) && !empty($_GET["tags"])) 
		$tags = split(",", $_GET["tags"]);
	if(!is_null($_GET["levels"]) && !empty($_GET["levels"]))
		$levels = split(",", $_GET["levels"]);
	if(!is_null($_GET["types"]) && !empty($_GET["types"]))
		$types = split(",", $_GET["types"]);
	
	$startIndex = $_GET["pageNum"]*15;
	$limit = 15;
	
	$dbParams = parse_ini_file('db.properties');

	$dbhost = $dbParams['host'];
	$dbuser = $dbParams['user'];
	$dbpass = $dbParams['pwd'];

	$conn = mysql_connect($dbhost, $dbuser, $dbpass)
	or die ('Error connecting to mysql ' . mysql_error());

	$dbname = $dbParams['name'];
	mysql_select_db($dbname);

	$statement = 'select v.video_id from video v, video_tag vt, tag t where ';
	$statement .= '(';
	$first = true;
	$tCount = 0;
	if($tags) {
		foreach($tags as $tag) {
			if($first)
				$first = false;
			else
				$statement .= " or ";
			$statement .= "t.name='" . $tag . "'";
			$tCount++;
		}
	}
	if($levels) {
		$tCount++;
		foreach($levels as $level) {
			if($first)
				$first = false;
			else
				$statement .= " or ";
			$statement .= "t.name='" . $level . "'";
		}
	}
	if($types) {
		$tCount++;
		foreach($types as $type) {
			if($first)
				$first = false;
			else
				$statement .= " or ";
			$statement .= "t.name='" . $type . "'";
		}
	}
	$statement .= ') and v.id=vt.video_id and t.id=vt.tag_id group by v.id having count(t.id)=' 
					. $tCount;
	// сначала узнаем общее количество записей
	$res = mysql_query($statement);
	echo mysql_num_rows($res) . '&';
					
	$statement .= ' limit ' . $startIndex . ', ' . $limit;
	$res = mysql_query($statement);
	$first = true;
	$ech = "";
	while($video = mysql_fetch_array($res)) {
		if($first)
			$first = false;
		else
			$eac .= ",";
		$eac .= $video[0];
	}
    if (substr($eac, -1) === ",") {
		$eac = substr($eac,'',-1);
	}
	echo $eac;
	echo '&' . $statement;
	
	mysql_free_result($res);
	mysql_close($conn);
}

$f = $_GET["function"];
if($f == 'saveVideo')
	saveVideo();
else if($f == 'getVideo')
	getVideo();
?>